package Model;

import java.awt.event.MouseAdapter;
import java.util.ArrayList;
import java.util.Date;

import Controller.Tasker;
import Date.ArrangeView;
import Date.Day;
import Events.Appointment;
import Events.EventParams;
import Events.GenericEvent;
import Events.MoodleEvent;
import Events.Recurrence;

public class DAOEventToFile {
	private DAOEvent dAOEvent;
	private DAOObjectIndex dAOObjectIndex;
	private DAORecuRemiWriter dAORecuRemiWriter;
	private Tasker tasker;
	public DAOEventToFile(Tasker tasker)
	{
		this.tasker=tasker;
		dAOEvent = new DAOEvent(this);
		dAORecuRemiWriter = new DAORecuRemiWriter(this);
	}
	public void getLastIndex() {
		// TODO Auto-generated method stub
		
	}
	public int getID() {
		// TODO Auto-generated method stub
		return tasker.getID();
	}
	public void sumID() {
		tasker.sumID();
	}
	public ArrayList<Index> getMonth(int i, int j)
	{
		  return dAOObjectIndex.getMonth(i, j);
	}
	public void setID(int i) {
		tasker.setID(i);
	}
	public void setDayMonth(ArrayList<Index> day) {
		// TODO Auto-generated method stub
		dAOObjectIndex = new DAOObjectIndex(day);
	}
	public synchronized boolean saveEvent(GenericEvent event, ArrayList<Index> day, boolean b) {
		// TODO Auto-generated method stub
		dAOEvent.saveEvent(event);
		if(event instanceof EventParams)
		{
			dAORecuRemiWriter.saveRecuminder(((EventParams)event).getReminder(), event.getIdEvent(), 1);
			dAORecuRemiWriter.saveRecuminder(((EventParams)event).getRecurrence(), event.getIdEvent(), 0);
		}
		dAOObjectIndex.SaveIndexMonth(event, day, b);
		dAOEvent.saveLastIndex();
		return true;
	}
	public ArrayList<GenericEvent> getEvents(ArrangeView arrangeView) {
		// TODO Auto-generated method stub
		return dAOEvent.getEvents(arrangeView);
	}
	
	public void updateCalendarEvents(ArrayList<GenericEvent> events2) {
		fuera: for(GenericEvent ge : events2)
		{
			Appointment me = (Appointment)ge;
			ArrayList<Index> month = dAOObjectIndex.getMonth(0, me.getSaveTime()); 
			for(int i=0; i<month.size(); i++)
			{
				if(month.get(i).getIndex2().equals(me.getParams().get("IDME")))
				{
					continue fuera;
				}
			}
			saveEvent(me, month, true);
//			month.add(new Index(me.getTime_Start(), me.getIdEvent()));
//			month.get(month.size()-1).setIndex2(me.getParams().get("IDME").toString());
		}
	}
	public Object readRRT(int idEvent, int i) {
		// TODO Auto-generated method stub
		return dAORecuRemiWriter.readRecuminder(idEvent, i);
	}
	

}
